package view;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import model.GenerationListener;
import model.GeneticAlgorithmDriver;
import model.StaticSizePopulation;

public class PopulationStringGrid extends JPanel implements GenerationListener
{
	StaticSizePopulation population;
	ArrayList<OrganismLabel> organismLabels;
	
	public PopulationStringGrid(StaticSizePopulation staticSizePopulation)
	{
		this.population = staticSizePopulation;
		this.organismLabels = new ArrayList<OrganismLabel>(population.getPopulationSize());
		int numRows = staticSizePopulation.getPopulationSize()/10;
		if(staticSizePopulation.getPopulationSize()%10 != 0)
		{
			numRows++;
		}
		
		this.setLayout(new GridLayout(staticSizePopulation.getPopulationSize()/10, 10));
		for(int i = 0; i < staticSizePopulation.getPopulationSize(); i++)
		{
			OrganismLabel organismLabel = new OrganismLabel(staticSizePopulation.getOrganismAtIndex(i));
			organismLabels.add(organismLabel);
			this.add(organismLabel);
		}
	}
	
	public void setPopulation(StaticSizePopulation population)
	{
		this.population = population;
	}

	@Override
	public void matingSeason(GeneticAlgorithmDriver geneticAlgorithm)
	{
		this.population = (StaticSizePopulation)geneticAlgorithm.getPopulation();
		for(int i = 0; i < this.getComponentCount(); i++)
		{
			OrganismLabel labelAtI = organismLabels.get(i);
			labelAtI.setOrganism(this.population.getOrganismAtIndex(i));
			labelAtI.repaint();
		}
		
	}
}
